const chatService = {
  state: {
    users: [],
    messages: {}
  },

  mutations: {
    INIT_MESSAGE_INFO: (state, { users, userMessages }) => {
      state.users = users
      if (userMessages === undefined) {
        for (let i = 0; i < users.length; i++) {
          state.messages[users[i].toString()] = []
        }
      } else {
        for (let i = 0; i < users.length; i++) {
          state.messages[users[i].toString()] = userMessages[i]
        }
      }
    }
  },

  actions: {
    InitMessageInfo({ commit }, users) {
      return new Promise(resolve => {
        commit('INIT_MESSAGE_INFO', { users, undefined })
        resolve()
      })
    },
    GetMessageListByUser({ commit, state }, user) {
      return new Promise((resolve) => {
        resolve(state.messages[user.toString()])
      })
    }
  }
}

export default chatService
